# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=rust
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.2.0
pkgrel=1
pkgdesc="A safe, concurrent, practical language from Mozilla (mingw-w64)"
arch=('any')
url="https://www.rust-lang.org/"
license=('custom:MIT' 'Apache')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("git"
             "bison"
             "flex"
             "${MINGW_PACKAGE_PREFIX}-curl"
             "${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-llvm"
             "${MINGW_PACKAGE_PREFIX}-libffi"
             "${MINGW_PACKAGE_PREFIX}-python2")
options=('!makeflags' 'staticlibs')
#install=rust.install
source=(https://static.rust-lang.org/dist/${_realname}c-${pkgver}-src.tar.gz)
sha256sums=('ea6eb983daf2a073df57186a58f0d4ce0e85c711bec13c627a8c85d51b6a6d78')

prepare() {
  cd ${srcdir}/${_realname}c-$pkgver
  #sed -e "s|CFG_WINDOWSY_\$(1)|0|g" -i mk/rustllvm.mk
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  #export CFG_LLVM_ROOT=${MINGW_PREFIX}
  cd "${srcdir}/build-${MINGW_CHOST}"
  ../${_realname}c-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --target=${MINGW_CHOST} \
    --llvm-root=${MINGW_PREFIX} \
    --release-channel=stable

    #--enable-local-rust              use an installed rustc rather than downloading a snapshot
    #--local-rust-root=[/usr/local]   set prefix for local rust binary
    #--enable-clang                   prefer clang to gcc for building the runtime

    make VERBOSE=1
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install
  
  rm -f ${pkgdir}${MINGW_PREFIX}/bin/libgcc*.dll
  rm -f ${pkgdir}${MINGW_PREFIX}/bin/libstd*.dll
}
